{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Slowest Key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: slowestKey"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #按键持续时间最长的键"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>LeetCode 设计了一款新式键盘，正在测试其可用性。测试人员将会点击一系列键（总计 <code>n</code> 个），每次一个。</p>\n",
    "\n",
    "<p>给你一个长度为 <code>n</code> 的字符串 <code>keysPressed</code> ，其中 <code>keysPressed[i]</code> 表示测试序列中第 <code>i</code> 个被按下的键。<code>releaseTimes</code> 是一个升序排列的列表，其中 <code>releaseTimes[i]</code> 表示松开第 <code>i</code> 个键的时间。字符串和数组的 <strong>下标都从 0 开始</strong> 。第 <code>0</code> 个键在时间为 <code>0</code> 时被按下，接下来每个键都 <strong>恰好</strong> 在前一个键松开时被按下。</p>\n",
    "\n",
    "<p>测试人员想要找出按键 <strong>持续时间最长</strong> 的键。第 <code>i</code><sup> </sup>次按键的持续时间为 <code>releaseTimes[i] - releaseTimes[i - 1]</code> ，第 <code>0</code> 次按键的持续时间为 <code>releaseTimes[0]</code> 。</p>\n",
    "\n",
    "<p>注意，测试期间，同一个键可以在不同时刻被多次按下，而每次的持续时间都可能不同。</p>\n",
    "\n",
    "<p>请返回单次按键 <strong>持续时间最长</strong> 的键，如果有多个这样的键，则返回 <strong>按字母顺序排列最大</strong> 的那个键。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>releaseTimes = [9,29,49,50], keysPressed = \"cbcd\"\n",
    "<strong>输出：</strong>\"c\"\n",
    "<strong>解释：</strong>按键顺序和持续时间如下：\n",
    "按下 'c' ，持续时间 9（时间 0 按下，时间 9 松开）\n",
    "按下 'b' ，持续时间 29 - 9 = 20（松开上一个键的时间 9 按下，时间 29 松开）\n",
    "按下 'c' ，持续时间 49 - 29 = 20（松开上一个键的时间 29 按下，时间 49 松开）\n",
    "按下 'd' ，持续时间 50 - 49 = 1（松开上一个键的时间 49 按下，时间 50 松开）\n",
    "按键持续时间最长的键是 'b' 和 'c'（第二次按下时），持续时间都是 20\n",
    "'c' 按字母顺序排列比 'b' 大，所以答案是 'c'\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>releaseTimes = [12,23,36,46,62], keysPressed = \"spuda\"\n",
    "<strong>输出：</strong>\"a\"\n",
    "<strong>解释：</strong>按键顺序和持续时间如下：\n",
    "按下 's' ，持续时间 12\n",
    "按下 'p' ，持续时间 23 - 12 = 11\n",
    "按下 'u' ，持续时间 36 - 23 = 13\n",
    "按下 'd' ，持续时间 46 - 36 = 10\n",
    "按下 'a' ，持续时间 62 - 46 = 16\n",
    "按键持续时间最长的键是 'a' ，持续时间 16</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>releaseTimes.length == n</code></li>\n",
    "\t<li><code>keysPressed.length == n</code></li>\n",
    "\t<li><code>2 &lt;= n &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= releaseTimes[i] &lt;= 10<sup>9</sup></code></li>\n",
    "\t<li><code>releaseTimes[i] &lt; releaseTimes[i+1]</code></li>\n",
    "\t<li><code>keysPressed</code> 仅由小写英文字母组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [slowest-key](https://leetcode.cn/problems/slowest-key/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [slowest-key](https://leetcode.cn/problems/slowest-key/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[9,29,49,50]\\n\"cbcd\"', '[12,23,36,46,62]\\n\"spuda\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        keysPressed_duration = [releaseTimes[0]]*len(releaseTimes)\n",
    "        keysPressed_duration[1::] = [(releaseTimes[i] - releaseTimes[i-1]) for i in range(1,len(releaseTimes))]\n",
    "        rs = sorted([keysPressed[i] for i in range(len(keysPressed_duration)) if keysPressed_duration[i]==max(keysPressed_duration)])\n",
    "        return rs[-1]\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        during_time = 0\n",
    "        ret_idx = \"\"\n",
    "        for i in range(0,len(keysPressed)):\n",
    "            if i == 0:\n",
    "                if releaseTimes[i] > during_time:\n",
    "                    ret_idx = keysPressed[i]\n",
    "                    during_time = releaseTimes[i]\n",
    "                elif releaseTimes[i] == during_time:\n",
    "                    ret_idx=max(keysPressed[i],ret_idx)\n",
    "                continue\n",
    "            if releaseTimes[i]-releaseTimes[i-1] > during_time:\n",
    "                ret_idx = keysPressed[i]\n",
    "                during_time = releaseTimes[i]-releaseTimes[i-1]\n",
    "            elif releaseTimes[i]-releaseTimes[i-1] == during_time:\n",
    "                ret_idx=max(keysPressed[i],ret_idx)\n",
    "        return ret_idx\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ans = keysPressed[0]\n",
    "        maxTime = releaseTimes[0]\n",
    "        for i in range(1,len(keysPressed)):\n",
    "            key = keysPressed[i]\n",
    "            time = releaseTimes[i]-releaseTimes[i-1]\n",
    "            if time>maxTime or time==maxTime and key>ans:\n",
    "                ans=key\n",
    "                maxTime = time\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        keysPressed_duration = [(releaseTimes[i] - releaseTimes[i-1]) for i in range(len(releaseTimes))]\n",
    "        keysPressed_duration[0] = releaseTimes[0]\n",
    "        rs = sorted([keysPressed[i] for i in range(len(keysPressed_duration)) if keysPressed_duration[i]==max(keysPressed_duration)])\n",
    "        return rs[-1]\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ans = keysPressed[0]\n",
    "        maxTime = releaseTimes[0]\n",
    "        for i in range(1, len(keysPressed)):\n",
    "            key = keysPressed[i]\n",
    "            time = releaseTimes[i] - releaseTimes[i - 1]\n",
    "            if time > maxTime or time == maxTime and key > ans:\n",
    "                ans = key\n",
    "                maxTime = time\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        keysPressed_duration = [releaseTimes[0]]*len(releaseTimes)\n",
    "        keysPressed_duration[1::] = [(releaseTimes[i] - releaseTimes[i-1]) for i in range(1,len(releaseTimes))]\n",
    "        rs = sorted([keysPressed[i] for i in range(len(keysPressed_duration)) if keysPressed_duration[i]==max(keysPressed_duration)])\n",
    "        return rs[-1]\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        act_time = [releaseTimes[0]] + [releaseTimes[i] - releaseTimes[i-1] for i in range(1, len(releaseTimes))]\n",
    "        time_dic = {}\n",
    "        \n",
    "        for i in range(0, len(keysPressed)):\n",
    "            if act_time[i] not in time_dic:\n",
    "                time_dic[act_time[i]] = keysPressed[i]\n",
    "            else:\n",
    "                time_dic[act_time[i]] += keysPressed[i]\n",
    "\n",
    "        max_button = max(time_dic)\n",
    "\n",
    "        return max(time_dic[max_button])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        releaseTimes.insert(0, 0)  # 左侧添加 0 便于计算\n",
    "        character, maxtime = '', 0\n",
    "        for i in range(1, len(releaseTimes)):\n",
    "            if releaseTimes[i] - releaseTimes[i - 1] > maxtime:\n",
    "                maxtime = releaseTimes[i] - releaseTimes[i - 1]\n",
    "                character = keysPressed[i - 1]\n",
    "            elif releaseTimes[i] - releaseTimes[i - 1] == maxtime:\n",
    "                character = character if character > keysPressed[i - 1] else keysPressed[i - 1]\n",
    "        return character"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ds = {}\n",
    "        key,value = '',0\n",
    "        for i in range(len(keysPressed)):\n",
    "            if i == 0:\n",
    "                ds[keysPressed[i]] = releaseTimes[i]\n",
    "            else:\n",
    "                ds[keysPressed[i]] = ds.get(keysPressed[i],0)\n",
    "                ds[keysPressed[i]] = max(ds[keysPressed[i]],releaseTimes[i]-releaseTimes[i-1])\n",
    "            if ds[keysPressed[i]] > value:\n",
    "                value = ds[keysPressed[i]]\n",
    "                key = keysPressed[i]\n",
    "            elif ds[keysPressed[i]] == value and keysPressed[i] > key:\n",
    "                key = keysPressed[i]\n",
    "        return key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        b = [0] * len(releaseTimes)\n",
    "        b[0] = releaseTimes[0]\n",
    "        for i in range(1, len(releaseTimes)):\n",
    "            b[i] = releaseTimes[i] - releaseTimes[i - 1]\n",
    "        maxtime = max(b)\n",
    "        arr = []\n",
    "        for i in range(len(releaseTimes)):\n",
    "            if b[i] == maxtime:\n",
    "                arr.append(i)\n",
    "        ans = []\n",
    "        for i in range(len(arr)):\n",
    "            ans.append(keysPressed[arr[i]])\n",
    "        return max(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        pressedTimes = [releaseTimes[0]] + [releaseTimes[i] - releaseTimes[i - 1] for i in range(1, len(releaseTimes))]\n",
    "        key = keysPressed[0]\n",
    "        time = 0\n",
    "        for i in range(len(keysPressed)):\n",
    "            if pressedTimes[i] > time:\n",
    "                key = keysPressed[i]\n",
    "                time = pressedTimes[i]\n",
    "            elif pressedTimes[i] == time:\n",
    "                if key < keysPressed[i]:\n",
    "                    key = keysPressed[i]\n",
    "        return key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ans = keysPressed[0]\n",
    "        maxTime = releaseTimes[0]\n",
    "        for i in range(1, len(keysPressed)):\n",
    "            key = keysPressed[i]\n",
    "            time = releaseTimes[i] - releaseTimes[i - 1]\n",
    "            if time > maxTime or time == maxTime and key > ans:\n",
    "                ans = key\n",
    "                maxTime = time\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        max_interval = releaseTimes[0]\n",
    "        ans_list = [keysPressed[0]]\n",
    "        for i in range(1, len(releaseTimes)):\n",
    "            interval = releaseTimes[i] - releaseTimes[i-1]\n",
    "            if interval > max_interval:\n",
    "                max_interval = interval\n",
    "                ans_list = [keysPressed[i]]\n",
    "            elif interval == max_interval:\n",
    "                ans_list.append(keysPressed[i])\n",
    "        ans_list.sort()\n",
    "        return ans_list[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        \"\"\"\n",
    "        keysPressed_duration = [releaseTimes[0]]*len(releaseTimes)\n",
    "        keysPressed_duration[1::] = [(releaseTimes[i] - releaseTimes[i-1]) for i in range(1,len(releaseTimes))]\n",
    "        rs = sorted([keysPressed[i] for i in range(len(keysPressed_duration)) if keysPressed_duration[i]==max(keysPressed_duration)])\n",
    "        return rs[-1]\n",
    "        \"\"\"\n",
    "        long_duration = releaseTimes[0]\n",
    "        index = 0\n",
    "        for i in range(1,len(keysPressed)):\n",
    "            duration = releaseTimes[i] - releaseTimes[i-1]\n",
    "            if duration>long_duration or (duration==long_duration and ord(keysPressed[i])>ord(keysPressed[index])):\n",
    "                index = i\n",
    "                long_duration = duration\n",
    "        return keysPressed[index]\n",
    "\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        k=[-1 for i in range(26)]\n",
    "        maxx=0\n",
    "        maxans=''\n",
    "        for i,a in enumerate(releaseTimes):\n",
    "            if i==0:\n",
    "                maxx=a\n",
    "                maxans=keysPressed[i]\n",
    "            else:\n",
    "                t=a-releaseTimes[i-1]\n",
    "                if t>maxx:\n",
    "                    maxx=t\n",
    "                    maxans=keysPressed[i]\n",
    "                if t==maxx and keysPressed[i]>maxans:\n",
    "                    maxans=keysPressed[i]\n",
    "        return maxans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ds = {}\n",
    "        for i in range(len(keysPressed)):\n",
    "            if i == 0:\n",
    "                ds[keysPressed[i]] = releaseTimes[i]\n",
    "            else:\n",
    "                ds[keysPressed[i]] = ds.get(keysPressed[i],0)\n",
    "                ds[keysPressed[i]] = max(ds[keysPressed[i]],releaseTimes[i]-releaseTimes[i-1])\n",
    "        key,value = '',0\n",
    "        for k,v in ds.items():\n",
    "            if v > value:\n",
    "                value = v \n",
    "                key = k \n",
    "            elif v == value and k > key:\n",
    "                key = k \n",
    "        return key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        tmp = [releaseTimes[i]-(0 if i==0 else releaseTimes[i-1]) for i in range(len(releaseTimes))]\n",
    "        max_count = max(tmp)\n",
    "        ans = -1\n",
    "        for i in range(len(tmp)):\n",
    "            if tmp[i]==max_count and (ans == -1 or keysPressed[ans]<keysPressed[i]):\n",
    "                ans = i\n",
    "        return keysPressed[ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        max, index = releaseTimes[0], 0\n",
    "        for i in range(1,len(releaseTimes)):\n",
    "            if (max == releaseTimes[i] - releaseTimes[i-1] and keysPressed[i]>keysPressed[index]) or (max < releaseTimes[i] - releaseTimes[i-1]):\n",
    "                max, index = releaseTimes[i] - releaseTimes[i - 1], i\n",
    "        print(max,index)\n",
    "        return keysPressed[index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        releaseTimes = [0]+releaseTimes\n",
    "        times = []\n",
    "        for i in range(1,len(releaseTimes)):\n",
    "            times.append(releaseTimes[i]-releaseTimes[i-1])\n",
    "        times.reverse()\n",
    "        keysPressed = keysPressed[::-1]\n",
    "        max1 = max(times)\n",
    "        res = []\n",
    "        for i in range(len(times)):\n",
    "            if times[i]==max1:\n",
    "                res.append(keysPressed[i])\n",
    "        return max(res)\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "         max_duration = releaseTimes[0]\n",
    "         max_key = keysPressed[0]\n",
    "         for i in range(1, len(releaseTimes)):\n",
    "             duration = releaseTimes[i] - releaseTimes[i-1]\n",
    "             if duration > max_duration:\n",
    "                 max_duration = duration\n",
    "                 max_key = keysPressed[i]\n",
    "             elif duration == max_duration and keysPressed[i] > max_key:\n",
    "                 max_key = keysPressed[i]\n",
    "         return max_key\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        n=len(keysPressed)\n",
    "        maxword=[keysPressed[0]]\n",
    "        maxtime=releaseTimes[0]\n",
    "        for i in range (1,n):\n",
    "            if releaseTimes[i]- releaseTimes[i-1]>maxtime:\n",
    "                maxword=[keysPressed[i]]\n",
    "                maxtime=releaseTimes[i]-releaseTimes[i-1]\n",
    "            elif releaseTimes[i]-releaseTimes[i-1]==maxtime:\n",
    "                maxword.append(keysPressed[i])\n",
    "        return max(maxword)\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        a=0\n",
    "        list_time=[]\n",
    "        max_zf_zb=[]\n",
    "        max_zf=[]\n",
    "        ascll_zf=[]\n",
    "        for i in releaseTimes:\n",
    "            list_time.append(i-a)\n",
    "            a=i\n",
    "        max_time=max(list_time)\n",
    "        for j in range(len(list_time)):\n",
    "            if list_time[j]==max_time:\n",
    "                max_zf_zb.append(j)\n",
    "        for k in max_zf_zb:\n",
    "            max_zf.append(keysPressed[k])\n",
    "        for l in max_zf:\n",
    "            ascll_zf.append(ord(l))\n",
    "        return chr(max(ascll_zf))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, a: List[int], b: str) -> str:\n",
    "        ret = b[0]\n",
    "        t = a[0]\n",
    "        for i in range(1, len(a)):\n",
    "            d = a[i] - a[i - 1]\n",
    "            if d > t or (d == t and b[i] > ret):\n",
    "                ret = b[i]\n",
    "                t = d\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        res = ''\n",
    "        pre = 0\n",
    "        max_time = 0\n",
    "        for i, num in enumerate(releaseTimes):\n",
    "            if releaseTimes[i]-pre > max_time:\n",
    "                res = keysPressed[i]\n",
    "                max_time = releaseTimes[i]-pre\n",
    "            elif releaseTimes[i]-pre == max_time:\n",
    "                res = max(res, keysPressed[i])\n",
    "            pre = releaseTimes[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        res, tmp = keysPressed[0], releaseTimes[0]\n",
    "        for i in range(1,len(releaseTimes)):\n",
    "            if releaseTimes[i] - releaseTimes[i-1] > tmp:\n",
    "                res, tmp = keysPressed[i], releaseTimes[i] - releaseTimes[i-1]\n",
    "            elif releaseTimes[i] - releaseTimes[i-1] == tmp and ord(keysPressed[i]) > ord(res):\n",
    "                res = keysPressed[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes:list[int], keysPressed: str) -> str:\n",
    "        maxval = releaseTimes[0]\n",
    "        ans = keysPressed[0]\n",
    "        for i in range(1,len(releaseTimes)):\n",
    "            res = releaseTimes[i] - releaseTimes[i-1]\n",
    "            if res > maxval or (res == maxval and keysPressed[i] > ans):\n",
    "                maxval = res\n",
    "                ans = keysPressed[i]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        time={keysPressed[0]:releaseTimes[0]}\n",
    "        x=keysPressed[0]\n",
    "        y=releaseTimes[0]\n",
    "        for i in range(1,len(releaseTimes)):\n",
    "            if keysPressed[i] not in time:\n",
    "                time[keysPressed[i]]=releaseTimes[i]-releaseTimes[i-1]\n",
    "            else:\n",
    "                time[keysPressed[i]]=max(time[keysPressed[i]],releaseTimes[i]-releaseTimes[i-1])\n",
    "            if y<time[keysPressed[i]]:\n",
    "                x=keysPressed[i]\n",
    "                y=time[keysPressed[i]]\n",
    "            elif y==time[keysPressed[i]] and keysPressed[i]>x:\n",
    "                x=keysPressed[i]\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ans = keysPressed[0]\n",
    "        max_time = releaseTimes[0]\n",
    "        for i in range(1, len(releaseTimes)):\n",
    "            if releaseTimes[i] - releaseTimes[i - 1] > max_time:\n",
    "                max_time = releaseTimes[i] - releaseTimes[i - 1]\n",
    "                ans = keysPressed[i]\n",
    "            elif releaseTimes[i] - releaseTimes[i - 1] == max_time:\n",
    "                if keysPressed[i] > ans:\n",
    "                    ans = keysPressed[i]\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        keysPressed_duration = [releaseTimes[0]]*len(releaseTimes)\n",
    "        keysPressed_duration[1::] = [(releaseTimes[i] - releaseTimes[i-1]) for i in range(1,len(releaseTimes))]\n",
    "        rs = [ord(keysPressed[i]) for i in range(len(keysPressed_duration)) if keysPressed_duration[i]==max(keysPressed_duration)]\n",
    "        return chr(max(rs))\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ans = keysPressed[0]\n",
    "        maxTime = releaseTimes[0]\n",
    "        for i in range(1,len(keysPressed)):\n",
    "            key = keysPressed[i]\n",
    "            time = releaseTimes[i]-releaseTimes[i-1]\n",
    "            if time>maxTime or time==maxTime and key>ans:\n",
    "                ans = key\n",
    "                maxTime = time\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "    l = len(releaseTimes)\n",
    "    rt, rv = releaseTimes[0], keysPressed[0]\n",
    "    for i in range(1, l):\n",
    "      t = releaseTimes[i] - releaseTimes[i - 1]\n",
    "      v = keysPressed[i]\n",
    "      if t > rt:\n",
    "        rt = t\n",
    "        rv = v\n",
    "      elif t == rt and v > rv:\n",
    "        # print(f\"udpate {rv} to {v}\")\n",
    "        rv = v\n",
    "    return rv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ans, m = None, 0\n",
    "        releaseTimes = [0] + releaseTimes\n",
    "        for i in range(len(keysPressed)):\n",
    "            n = releaseTimes[i + 1] - releaseTimes[i]\n",
    "            if n > m:\n",
    "                ans = keysPressed[i]\n",
    "                m = n\n",
    "            elif n == m:\n",
    "                ans = max(ans, keysPressed[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        cur = (0, 'a')\n",
    "        for i in range(len(releaseTimes)):\n",
    "            if i == 0:\n",
    "                p = (releaseTimes[i], keysPressed[i])\n",
    "            else:\n",
    "                p = (releaseTimes[i] - releaseTimes[i - 1], keysPressed[i])\n",
    "            if p > cur:\n",
    "                cur = p\n",
    "        return cur[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        record = [releaseTimes[0]]\n",
    "        record.extend([releaseTimes[index] - releaseTimes[index - 1] for index in range(1, len(releaseTimes))])\n",
    "        maxnum = max(record)\n",
    "        res = [keysPressed[i] for i, number in enumerate(record) if number == maxnum]\n",
    "        res.sort()\n",
    "        return res[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ans = keysPressed[0]\n",
    "        maxTime = releaseTimes[0]\n",
    "        for i in range(1, len(keysPressed)):\n",
    "            key = keysPressed[i]\n",
    "            time = releaseTimes[i] - releaseTimes[i - 1]\n",
    "            if time > maxTime or time == maxTime and key > ans:\n",
    "                ans = key\n",
    "                maxTime = time\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ds = {}\n",
    "        key,value = '',0\n",
    "        for i in range(len(keysPressed)):\n",
    "            if i == 0:\n",
    "                ds[keysPressed[i]] = releaseTimes[i]\n",
    "            else:\n",
    "                ds[keysPressed[i]] = ds.get(keysPressed[i],0)\n",
    "                ds[keysPressed[i]] = max(ds[keysPressed[i]],releaseTimes[i]-releaseTimes[i-1])\n",
    "            if ds[keysPressed[i]] > value:\n",
    "                value = ds[keysPressed[i]]\n",
    "                key = keysPressed[i]\n",
    "            elif ds[keysPressed[i]] == value and keysPressed[i] > key:\n",
    "                key = keysPressed[i]\n",
    "        return key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        pre_time = 0\n",
    "        longest_press_time = 0\n",
    "        longest_key = keysPressed[0]\n",
    "        for key_index, release_time in enumerate(releaseTimes):\n",
    "            cur_time = release_time - pre_time\n",
    "            if cur_time > longest_press_time:\n",
    "                longest_press_time = cur_time\n",
    "                longest_key = keysPressed[key_index]\n",
    "            elif cur_time == longest_press_time:\n",
    "                longest_key = max(longest_key, keysPressed[key_index])\n",
    "            pre_time = release_time\n",
    "        return longest_key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        releaseTimes.insert(0,0)\n",
    "        for i in range(1,len(releaseTimes)):\n",
    "            releaseTimes[i-1] = releaseTimes[i]-releaseTimes[i-1]\n",
    "        releaseTimes = releaseTimes[:-1]\n",
    "        target = -float(\"inf\")\n",
    "        temp = []\n",
    "        for index in range(len(releaseTimes)):\n",
    "            if releaseTimes[index] > target:\n",
    "                temp = [keysPressed[index]]\n",
    "                target = releaseTimes[index]\n",
    "            elif releaseTimes[index] == target:\n",
    "                temp.append(keysPressed[index])\n",
    "        print(temp)\n",
    "        return max(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ans = keysPressed[0]\n",
    "        maxTime = releaseTimes[0]\n",
    "        for i in range(1, len(keysPressed)):\n",
    "            key = keysPressed[i]\n",
    "            time = releaseTimes[i] - releaseTimes[i - 1]\n",
    "            if time > maxTime or time == maxTime and key > ans:\n",
    "                ans = key\n",
    "                maxTime = time\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ts = [releaseTimes[0]] + [releaseTimes[i] - releaseTimes[i-1] for i in range(1,len(releaseTimes))]\n",
    "        ds = {}\n",
    "        for i in range(len(keysPressed)):\n",
    "            ds[keysPressed[i]] = ds.get(keysPressed[i],0)\n",
    "            ds[keysPressed[i]] = max(ts[i],ds[keysPressed[i]])\n",
    "        key,value = '',0\n",
    "        for k,v in ds.items():\n",
    "            if v > value:\n",
    "                value = v \n",
    "                key = k \n",
    "            elif v == value and k > key:\n",
    "                key = k \n",
    "        return key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        releaseTimes = [0] + releaseTimes \n",
    "        last_ = -1 \n",
    "        ans = \"\"\n",
    "        for i , x in enumerate(pairwise(releaseTimes)) :\n",
    "            a , b = x[0],x[1]\n",
    "            if last_ == b - a and ord(ans) < ord(keysPressed[i]) :\n",
    "                ans = keysPressed[i] \n",
    "            elif last_ < b - a:\n",
    "                ans = keysPressed[i] \n",
    "                last_ = b - a \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        releaseTimes.insert(0,0)\n",
    "        for i in range(1,len(releaseTimes)):\n",
    "            releaseTimes[i-1] = releaseTimes[i]-releaseTimes[i-1]\n",
    "        releaseTimes = releaseTimes[:-1]\n",
    "        target = -float(\"inf\")\n",
    "        temp = []\n",
    "        for index in range(len(releaseTimes)):\n",
    "            if releaseTimes[index] > target:\n",
    "                temp = [keysPressed[index]]\n",
    "                target = releaseTimes[index]\n",
    "            elif releaseTimes[index] == target:\n",
    "                temp.append(keysPressed[index])\n",
    "        print(temp)\n",
    "        return max(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        mx_duration = 0\n",
    "        pre = 0\n",
    "        n = len(keysPressed)\n",
    "        ans = ''\n",
    "        for i in range(n):\n",
    "            tmp = releaseTimes[i] - pre\n",
    "            if tmp > mx_duration:\n",
    "                mx_duration = tmp \n",
    "                ans = keysPressed[i]\n",
    "            elif tmp == mx_duration:\n",
    "                if ans < keysPressed[i]:\n",
    "                    ans = keysPressed[i]\n",
    "            pre = releaseTimes[i]\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        longest_duration = releaseTimes[0]  # 初始化最长持续时间为第一个按键的时间\n",
    "        longest_key = keysPressed[0]       # 初始化最长持续时间对应的键为第一个按键\n",
    "        \n",
    "        for i in range(1, len(releaseTimes)):\n",
    "            duration = releaseTimes[i] - releaseTimes[i - 1]  # 计算当前按键的持续时间\n",
    "            if duration > longest_duration or (duration == longest_duration and keysPressed[i] > longest_key):\n",
    "                # 更新最长持续时间和对应的键\n",
    "                longest_duration = duration\n",
    "                longest_key = keysPressed[i]\n",
    "        \n",
    "        return longest_key\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        x=[[keysPressed[0],releaseTimes[0]]]\n",
    "        for i in range(1,len(keysPressed)):\n",
    "            y=[]\n",
    "            y=[keysPressed[i],releaseTimes[i]-releaseTimes[i-1]]\n",
    "            x.append(y)\n",
    "        s=sorted(x,key=lambda x:(x[1],x[0]),reverse=True)\n",
    "        return s[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        ad={}\n",
    "        for i in range(len(keysPressed)):\n",
    "            if i==0 and keysPressed[i] not in ad:\n",
    "                ad[keysPressed[i]]=[releaseTimes[i]]\n",
    "            if i>0 and keysPressed[i] not in ad:\n",
    "                ad[keysPressed[i]]=[releaseTimes[i]-releaseTimes[i-1]]\n",
    "            else:\n",
    "                ad[keysPressed[i]].append(releaseTimes[i]-releaseTimes[i-1])\n",
    "        for i in ad:\n",
    "            ad[i].sort()\n",
    "        r=[[k,v[-1]] for k,v in ad.items() ]\n",
    "        r.sort(key=lambda x:(x[1],x[0]))\n",
    "        return r[-1][0]        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        temp = {}\n",
    "        str_list = list(keysPressed)\n",
    "        for i in range(len(releaseTimes)):\n",
    "            key = keysPressed[i]\n",
    "            if i == 0:\n",
    "                value = releaseTimes[i]\n",
    "            else:\n",
    "                value = releaseTimes[i] - releaseTimes[i - 1]\n",
    "            if key not in temp.keys():\n",
    "                temp[key] = value\n",
    "            else:\n",
    "                temp[key] = max(value, temp[key])\n",
    "        res = []\n",
    "        for k, v in temp.items():\n",
    "            if v == max(temp.values()):\n",
    "                res.append(k)\n",
    "        return sorted(res)[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        n = len(keysPressed)\n",
    "        d = {}\n",
    "        for i in range(n):\n",
    "            if i == 0:\n",
    "                if keysPressed[i] in d:\n",
    "                    d[keysPressed[i]] = max(releaseTimes[i],d[keysPressed[i]])\n",
    "                else:\n",
    "                    d[keysPressed[i]] = releaseTimes[i]\n",
    "            else:\n",
    "                if keysPressed[i] in d:\n",
    "                    d[keysPressed[i]] = max(releaseTimes[i]-releaseTimes[i-1],d[keysPressed[i]])\n",
    "                else:\n",
    "                    d[keysPressed[i]] = releaseTimes[i]-releaseTimes[i-1]\n",
    "        #m_value = max(d, key=d.get)\n",
    "        m_value = max(d.values())\n",
    "        res = []\n",
    "        for k in d:\n",
    "            if d[k] == m_value:\n",
    "                res.append(k)\n",
    "        res.sort()\n",
    "        return res[-1]\n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        res=[]\n",
    "        releaseTimes=[0]+releaseTimes\n",
    "        counter=dict()\n",
    "        for i in range(len(keysPressed)):\n",
    "            counter[keysPressed[i]]=0\n",
    "        for i in range(len(keysPressed)):\n",
    "            counter[keysPressed[i]]=max(releaseTimes[i+1]-releaseTimes[i],counter[keysPressed[i]])\n",
    "        MAX=0\n",
    "        for key in counter:\n",
    "            MAX=max(counter[key],MAX)\n",
    "        for key in counter:\n",
    "            if counter[key]==MAX:\n",
    "                res+=[key]\n",
    "        res.sort()\n",
    "        return res[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import functools\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        # 测试人员将会点击一系列键（总计 n 个），每次一个\n",
    "        n = len(keysPressed)\n",
    "        # keysPressed[i] 表示测试序列中第 i 个被按下的键\n",
    "        # releaseTimes 是一个升序排列的列表，其中 releaseTimes[i] 表示松开第 i 个键的时间\n",
    "        # 第 0 个键在时间为 0 时被按下，接下来每个键都 恰好 在前一个键松开时被按下\n",
    "        # 测试人员想要找出按键 持续时间最长 的键,即前后相差最大的\n",
    "        # 注意，测试期间，同一个键可以在不同时刻被多次按下，而每次的持续时间都可能不同\n",
    "        # 请返回单次按键 持续时间最长 的键，如果有多个这样的键，则返回 按字母顺序排列最大 的那个键。\n",
    "        ll = []  # 存放被按的键及持续的时间\n",
    "        # 遍历 每个键\n",
    "        for k in range(n):\n",
    "            use_time = releaseTimes[0] if k == 0 else releaseTimes[k] - releaseTimes[k-1]\n",
    "            # 第 i 次按键的持续时间为 releaseTimes[i] - releaseTimes[i - 1]\n",
    "            if [keysPressed[k],use_time] not in ll:\n",
    "                ll.append([keysPressed[k],use_time])\n",
    "        # 按时长和字典序排序\n",
    "        def cmp(a,b):\n",
    "            if a[1] == b[1]:\n",
    "                return -1 if a[0] > b[0] else 1\n",
    "            return -1 if a[1] > b[1] else 1\n",
    "        ll.sort(key=functools.cmp_to_key(cmp))\n",
    "        return ll[0][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:\n",
    "        dict1 = {}\n",
    "        for i in range(len(releaseTimes)):\n",
    "            if i ==0:\n",
    "                dict1[keysPressed[i]]=releaseTimes[i]\n",
    "            else:\n",
    "                if dict1.get(keysPressed[i])==None:\n",
    "                    dict1[keysPressed[i]] = releaseTimes[i]-releaseTimes[i-1]\n",
    "                else:\n",
    "                    if releaseTimes[i]-releaseTimes[i-1]>dict1.get(keysPressed[i]):\n",
    "                        dict1[keysPressed[i]] = releaseTimes[i] - releaseTimes[i - 1]\n",
    "        print(dict1)\n",
    "        res = sorted(dict1.items(),key=lambda x:x[1],reverse=True)\n",
    "        print(res)\n",
    "        r1 = res[0][0]\n",
    "        for i in res:\n",
    "            if i[1]==res[0][1]:\n",
    "                if i[0]>r1:\n",
    "                    r1 = i[0]\n",
    "        return(r1)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
